package com.xm.xmwa1706090.service.service.impl;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xm.xm.dto.ResultDto;
import com.xm.xm.dto.SearchDto;
import com.xm.xm.util.ErrorCode;
import com.xm.xm.util.SmsUtils;
import com.xm.xm.util.ValidateUtils;
import com.xm.xmwa1706090.service.config.MsgConfig;
import com.xm.xmwa1706090.service.mapper.QbSmsMapper;
import com.xm.xmwa1706090.service.model.QbSms;
import com.xm.xmwa1706090.service.service.QbSmsService;
import com.xm.xmwa1706090.service.util.HttpSender;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Random;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 杭州新苗网络科技有限公司
 * @author meihf
 * @date 2017/07/07
 * @description
*/ 
@Service("qbSmsService")
public class QbSmsServiceImpl extends ServiceImpl<QbSmsMapper,QbSms> implements QbSmsService {


        @Autowired
        QbSmsMapper qbSmsMapper;

        @Override
        public ResultDto sendMessage(SearchDto searchDto) {
            //1是注册 2是找回密码 3是预约 4是添加我的车辆
            ReentrantLock reentrantLock = new ReentrantLock();
            reentrantLock.lock();
            ResultDto resultDto = new ResultDto();
            JSONObject filed = searchDto.getFiled();
            searchDto.setPageNo((searchDto.getPageNo()-1)*searchDto.getPageSize());
            try {
                if (!filed.has("mobile") || !filed.has("sendtype")){
                    resultDto.setMsg(ErrorCode.E10011);
                    resultDto.setCode(ErrorCode.C10011);
                    return resultDto;
                }else{
                    String phone = filed.getString("mobile");
                    //sendtype1是登录
                    int sendtype = filed.getInt("sendtype");
                    if(!ValidateUtils.isMobileNO(Long.parseLong(phone))){
                        resultDto.setCode(ErrorCode.C10004);
                        resultDto.setMsg(ErrorCode.E10004);
                        return  resultDto;
                    }

                    Random random = new Random();
                    Integer validateCode = random.nextInt(899999);
                    validateCode = validateCode + 100000;

                    String content = MsgConfig.PRODUCT;
                    //添加发送短信内容
                    if (sendtype == 2){
                        content += "您的验证码是:"+validateCode+"，您正在通过手机号重置登录密码，10分钟内有效,如非本人操作，请忽略该短信。";
                    }else {
                        content += "您的验证码是:"+validateCode+"，10分钟内有效。请不要把验证码泄漏给其他人，如非本人请勿操作。";
                    }
                    String uri = MsgConfig.URL;//应用地址
                    String account = MsgConfig.ACCOUNT;//账号
                    String pswd = MsgConfig.PASSWORD;//密码
                    String mobiles = phone;//手机号码，多个号码使用","分割
                    boolean needstatus = true;//是否需要状态报告，需要true，不需要false
                    String productId = MsgConfig.PRODUCTId;//产品ID(不用填写)
                    String extno = MsgConfig.EXTNO;//扩展码(请登陆网站用户中心——>服务管理找到签名对应的extno并填写，线下用户请为空)
                    ResultDto result = HttpSender.batchSend(uri, account, pswd, mobiles, content, needstatus, productId, extno);
                    if (result.getCode()>0){
                        return result;
                    }
                    //保存到表里面
                    QbSms qbSms = new QbSms();
                    qbSms.setContent(content);
                    qbSms.setCheckcode(String.valueOf(validateCode));
                    qbSms.setCreate_time(searchDto.getTime().intValue());
                    qbSms.setSendtype(sendtype);
                    qbSms.setMobile(String.valueOf(phone));
                    qbSms.setValidity(searchDto.getTime().intValue()+ SmsUtils.tenMinute().intValue());
                    qbSmsMapper.insert(qbSms);
                    resultDto.setMsg("短信发送成功");
                }
            }catch (Exception e ){
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                resultDto.setCode(ErrorCode.C10013);
                resultDto.setMsg(ErrorCode.E10013);
            }finally {
                reentrantLock.unlock();
            }
            return resultDto;
        }
}

